package com.tianfei.crowd.mvc.controller;

import com.tianfei.crowd.entity.Auth;
import com.tianfei.crowd.entity.Role;
import com.tianfei.crowd.service.api.AdminService;
import com.tianfei.crowd.service.api.AuthService;
import com.tianfei.crowd.service.api.RoleService;
import com.tianfei.crowd.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author: Herz
 * @date: 2021/7/17 20:33
 */
@Controller
public class AssignController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;


    @ResponseBody
    @RequestMapping("/assign-save-auth-to-role.json")
    public ResultEntity<String> saveAssignAuthToRole(
            @RequestBody Map<String,List<Integer>> map){

        authService.saveAssignAuthToRole(map);

        return ResultEntity.successWithOutData();
    }


    @ResponseBody
    @RequestMapping("/get-assigned-auth-id-by-role-id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam("roleId") Integer roleId){

        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);

        return ResultEntity.successWithData(authIdList);
    }



    @ResponseBody
    @RequestMapping("/assign-get-all-auth.json")
    public ResultEntity<List<Auth>> getAllAuth(){

       List<Auth> authList = authService.getAllAuth();

        return ResultEntity.successWithData(authList);
    }


    @RequestMapping("/save-admin-role-relationship.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,

            // 默认是不允许为空的，required = false 修改默认允许为空
            @RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList
    ) {

        adminService.saveAdminRoleRelationship(adminId, roleIdList);

        return "redirect:/admin-to-user-page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    @RequestMapping("/assign-to-role-page.html")
    public String toAssignRolePage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap
    ) {

        // 1、查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        // 2、查询未分配角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRole(adminId);

        // 3、存入模型（实质上是：request.setAttribute("attrName",attrValue)）
        modelMap.addAttribute("assignedRoleList", assignedRoleList);
        modelMap.addAttribute("unAssignedRoleList", unAssignedRoleList);

        return "assign/assign-role-admin";
    }
}
